package cli;

import filefinder.finders.Finder;
import filefinder.finders.GrepFinder;
import filefinder.finders.StringFinder;

public class Params {
	private int threadPoolSize;
	private String toFind;
	private String filePath;
	private Finder finder;

	private Params(String toFind, String filePath, Finder finder,
			int threadPoolSize) {
		this.toFind = toFind;
		this.filePath = filePath;
		this.finder = finder;
		this.threadPoolSize = threadPoolSize;
	};

	public int getThreadPoolSize() {
		return threadPoolSize;
	}

	public String getToFind() {
		return toFind;
	}

	public String getFilePath() {
		return filePath;
	}

	public Finder getFinder() {
		return finder;
	}
/**
 * Parsing String[] arguments according to following rules based on size of string array:<br>
 * 2 parameters : first parameter is start_directory, second is search_token<br>
 * 3 parameters : if first parameter is number treat it as thread size, otherwise if it string and equals to "reg" treat search_token as regular expression<br>
 * 		second parameter is start_directory<br>
 * 		third is search_token<br>  
 * 4 parameters : first parameter is regexp modifier and should be equal to "reg" to treat search_token as regular expression<br>
 * 	          second parameter should be number and specify number of threads<br>
 * 			  third parameter is start_directory<br>
 * 		      fourth is search_token<br>
 * If passed args array does not conform to described above method just print usage help 
 * 				
 * @param args array of parameters size from 2 to 4
 * @return
 */
	public static Params parse(String[] args) {
		if (args.length < 2) {
			usage("At least 2 parameters should be specified");
		}
		Params params = null;
		switch (args.length) {
		case 2:
			params = new Params(args[0], args[1], new StringFinder(), 1);
			break;
		case 3:
			try {
				params = new Params(args[1], args[2], new StringFinder(),
						Integer.parseInt(args[0]));
			} catch (NumberFormatException nfe) {
				if ("reg".equals(args[0])) {
					params = new Params(args[1], args[2], new GrepFinder(),
							Integer.parseInt(args[0]));
				} else {
					usage("");
				}
			}
			break;
		case 4:
			try {
				if ("reg".equals(args[0])) {
					params = new Params(args[2], args[3], new GrepFinder(),
							Integer.parseInt(args[1]));
				} else {
					usage("In case regexp string \"reg\" parameter should be specified");
				}
			} catch (NumberFormatException nfe) {
				usage("\"" + args[1] + "\" cannot be treated as numeric type");
			}
			break;
		default:
			usage("");
		}
		return params;
	}

	private static void usage(String string) {
		System.out
				.println("Recursive search for a string starting at provided root path");
		System.out
				.println("using specified number of threads: default is one ");
		System.out.println("	1. string is case sensitive");
		System.out.println("	2. symbolic links following is not yet supported");
		System.out
				.println("string is treated as regular expression if optional reg parameter is specified");
		System.out.println("Call syntax:");
		System.out.println("	[reg] [treadcount] search_string  root_path");
		System.out.println("example:");
		System.out.println("	reg 8 \"[abc]def\" /home/user/rbc");
		System.out.println("Error: ");
		System.out.println("	" + string);
		System.exit(0);
	}
}